package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Bill;
import study.saas.order.repo.BillRepo;
import study.saas.order.service.model.BillAddDTO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BillService {
    @Resource
    private BillRepo billRepo;

    public ApiResultV2<List<Bill>> selectAll() {
        List<Bill> list = billRepo.findAll();
        return ApiResultV2.ok(list);
    }

    public ApiResultV2<List<BillAddDTO>> select(BillAddDTO billAddDTO) {
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select * from Indent I where I.isDeleted = 0 ");
        if (!StringUtil.isEmpty(billAddDTO.getName())) {
            sql.append(" and I.CommodityName like :getName");
            param.put("getName", "%" +billAddDTO.getName()+ "%");
        }
        sql.append(" order by I.createTime desc,uuid desc ");
        List<BillAddDTO> list = billRepo.nativeSqlQuery(sql.toString(), param);
        return ApiResultV2.ok(list);
    }

    public Bill save(Bill entity) {
        return billRepo.save(entity);
    }
}
